/*
 * LinkDebug.c
 *
 *  Created on: 2012/08/13
 *      Author: guanxiaoying
 */
#include "../coredef.h"

static VOID printVariableName( AST astVari ){
	printf("%s", stVariableInfoAccess.name.get( astVari ));
}
static VOID printOperand( AST astOperand ){
	AST astConstant;
	switch( stOperandAccess.kind.get( astOperand )){
	case OPERAND_OBJECT :
		printf("<VAR(" );
		printVariableName( stOperandAccess.parameter.get( astOperand ));
		printf(")>" );
		break;
	case OPERAND_FUNCTION :
		printf("<FUN(%s)>", stFunctionInfoAccess.name.get( stOperandAccess.parameter.get( astOperand )) );
		break;
	case OPERAND_RESULT :
		printf("<RES(%ld)>", stOperandAccess.parameter.get( astOperand ) );
		break;
	case OPERAND_CONSTANT :
		astConstant = stOperandAccess.parameter.get( astOperand );
		if( stConstantAccess.isFloating( astConstant ) ){
			printf("<CON(%.3lf)>", stConstantAccess.getFloating( astConstant ));
		}else{
			printf("<CON(%s%llu)>", stConstantAccess.isNegative( astConstant )?"-":"",
					stConstantAccess.getInteger( astConstant ).u8Remainder );
		}
		break;
	case OPERAND_CONSTSTRING :
		astConstant = stOperandAccess.parameter.get( astOperand );
		printf("<STR(%s)>", stCcConstStringAccess.getStr( astConstant ));
		break;
	default :
		printf("<OTH(%ld)>", astOperand );
		break;
	}
}
S4 printCmdList( AST astCmdList, U4 event ){
	U4 kind = stCmdListAccess.opKind.get( astCmdList );
	S4 rc = 0;
	if( event == 1 ){
		printf( "PUSH ");
	}else if( event == 2 ){
		printf( "POP ");
	}
	switch(kind){
	case ASM_CMD_NOP :
		printf("NOP %s(%ld)\n", stCmdListAccess.label.get( astCmdList ), astCmdList );
		break;
	case ASM_CMD_RETURN  :
		printf("RET %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_CHOOSE :
		printf("CHOOSE %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_CHOOSEEND :
		printf("CHOOSEEND %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_CONFLUX  :
		printf("CONFLUX %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printf("\n" );
		break;
	case ASM_CMD_JUDGE:
		printf("JUDGE %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_JUMP  :
		printf("JUMP %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_BRANCH  :
		printf("BRANCH %s(%ld) ", stCmdListAccess.label.get( astCmdList ), astCmdList );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("\n" );
		break;
	case ASM_CMD_FUNCENTRY :
		printf("START %s(%ld)\n", stCmdListAccess.label.get( astCmdList ), astCmdList );
		break;
	case ASM_CMD_FUNCEXIT   :
		printf("EXIT %s(%ld)\n", stCmdListAccess.label.get( astCmdList ), astCmdList );
		break;
	case ASM_CMD_GETOPERAND  :
		printf("GET " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GETOFFSET_UNIT   :
		printf("OFFSET_U " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GETOFFSET    :
		printf("OFFSET " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GETMEMDATA   :
		printf("MEM " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GETADDR  :
		printf("ADR " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_CALL  :
		printf("CALL " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_INC  :
		printf("INC " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_DEC  :
		printf("DEC " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_MINUS  :
		printf("MINUS " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_NEGATION :
		printf("NEG " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_BITNEGATION :
		printf("BIT_NEG " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_CAST   :
		printf("CAST " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_MUL   :
		printf("MUL " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_DIV   :
		printf("DIV " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_MOD   :
		printf("MOD " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_ADD  :
		printf("ADD " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_SUB  :
		printf("SUB " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_LSFT   :
		printf("LSFT " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_RSFT   :
		printf("RSHT " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GREATER   :
		printf("> " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_GREATEREQUAL   :
		printf(">= " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_LESS   :
		printf("< " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_LESSEQUAL   :
		printf("<= " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_EQUAL   :
		printf("== " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_UNEQUAL   :
		printf("!= " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_BITAND   :
		printf("& " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_BITEXOR   :
		printf("^ " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_BITINOR  :
		printf("| " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_LGAND   :
		printf("&& " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_LGOR   :
		printf("|| " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_ASSIGN   :
		printf("= " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	case ASM_CMD_ASSIGNARG   :
		printf("ARG " );
		printOperand( stCmdListAccess.leftOperand.get( astCmdList ) );
		printOperand( stCmdListAccess.rightOperand.get( astCmdList ) );
		printf("(%ld)\n", astCmdList );
		break;
	default  :
		printf("***%lu (%ld)\n", kind, astCmdList );
		break;
	}
	return rc;
}
S4 printAllCmdList( VOID ){
	AST astMin = MIN_ARRAY;
	AST astMax = LinkCommandList.getMax();
	for( ; astMin <= astMax; astMin++ ){
		printCmdList( astMin , 0 );
	}
	return 0;
}
S4 printAllVariable( VOID ){
	AST astMin = MIN_ARRAY;
	AST astMax = LinkVariable.getMax();
	for( ; astMin <= astMax; astMin++ ){
		printVariableName( astMin  );
		printf("\n");
	}
	return 0;
}
